﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using WebVideo.Areas.Admin.Models;
using System.Collections;
using System.Web.Mvc;

namespace WebVideo.Areas.Admin.Controllers
{
    public class ListQuestionOfUserController : Controller
    {
        //
        // GET: /Admin/ListQuestionOfUser/

        public ActionResult Index_cu()
        {
            return View(ListQuestionOfUserModel.LayDuLieu());
        }
        public ActionResult Index(int IdQues)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            var query = db.EntityListQuestionOfUsers.Where(g => g.IdQuestion == IdQues).OrderByDescending(g => g.Date).ToList();

            return View(query);
        }
        #region "Point"
        public ActionResult Point(string UserName, int IdVideo)
        {
            return View(ListQuestionOfUserModel.LayDS(UserName,IdVideo ));
        }

        
        public ActionResult InsertPoint(string UserName, int IdVideo, string KQ)
        {

            try
            {
                if (KQ == "1")
                {

                    dbVideoDataContext db = new dbVideoDataContext();
                    EntityUser us = db.EntityUsers.Where(p => p.UserName == UserName).First();
                    EntityVideo vs = db.EntityVideos.Where(p => p.Id == IdVideo).First();

                    int Diem = int.Parse(vs.EntityTypeVideo.Point.ToString());
                    // bước 1 cộng điểm cho user
                    us.PointVideo = us.PointVideo + Diem;
                    //bước 2: cộng điểm cho người bạn đã mời user
                    if (us.UserNameRoot != null)
                    {
                        EntityUser us1 = db.EntityUsers.Where(g => g.UserName == us.UserNameRoot).First();
                        us1.PointFriend1 = us1.PointFriend1 + (Diem * 2) / 100;
                        if (us1.UserNameRoot != null)
                        {
                            EntityUser us2 = db.EntityUsers.Where(g => g.UserName == us1.UserNameRoot).First();
                            us2.PointFriend2 = us2.PointFriend1 + Diem / 100;
                        }
                    }

                    db.SubmitChanges();
                    // delete 
                    List<EntityListQuestionOfUser > LUS = db.EntityListQuestionOfUsers.Where(p => p.UserName == UserName && p.EntityQuestions_Essay.EntityVideo .Id == IdVideo ).ToList();
                    db.EntityListQuestionOfUsers.DeleteAllOnSubmit (LUS);
                    db.SubmitChanges();
                }

                return RedirectToAction("Index", "ListQuestionOfUser");
               
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
           
        }
        #endregion //insert
    }
}
